package com.netty.admin.controller;

import com.netty.admin.service.FriendRelationService;
import com.netty.admin.service.FriendService;
import com.netty.admin.service.UserService;
import com.netty.common.annotation.Log;
import com.netty.common.entity.bo.FriendFormBo;
import com.netty.common.entity.result.Result;
import com.netty.common.enums.BusinessType;
import com.netty.common.enums.ExceptionEnum;
import com.netty.common.enums.OperatorType;
import com.sun.corba.se.spi.activation._RepositoryStub;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * @author LiuXiaoYu
 * @date 2022/2/2- 20:05
 */
@RestController
@RequestMapping("/userFriend")
public class FriendController {

    @Autowired
    private FriendRelationService friendRelationService;



    @PostMapping("/addFriend")
    @Log(title = "添加好友",businessType = BusinessType.INSERT,operatorType = OperatorType.USER)
    public Result<?> addFriend(@RequestBody @Validated FriendFormBo friend){
       return friendRelationService.insert(friend);
    }

    @GetMapping("/getAllFriends")
    @Log(title = "获取所有好友",businessType = BusinessType.GET,operatorType = OperatorType.USER)
    public Result<?> getAllFriends(){
        return friendRelationService.getAll();
    }


    @GetMapping("/deleteFriend")
    @Log(title = "删除好友",businessType = BusinessType.DELETE,operatorType = OperatorType.USER)
    public Result<?> deleteFriend(@RequestParam(value = "userId")@NotNull(message = "用户id不能为空") Integer userId, @RequestParam("friendId")@NotNull(message = "好友id不能为空") Integer friendId){
        if (userId==null||friendId==null){
            return Result.error(ExceptionEnum.ERROR,"删除好友失败");
        }
        //双向删除
        return friendRelationService.deleteFriend(userId,friendId);
    }


}
